/**
 * create by ck on 2016/06/21.
 */
$(document).ready(function() {
    var schoolId    = xhTool.getQuery('iSchoolId'),
        inList      = $('#InList'),
        outList     = $('#OutList'),
        appCodeDom  = $('#sl_appCode'),
        DataGrid2   = new DataGridEvent(),
        appCode     = appCodeDom.val(),
        inParams    = {"iAppCode": appCode, "iSchoolId": schoolId, "type": "in"},
        outParams   = {"iAppCode": appCode, "iSchoolId": schoolId, "type": "out"},
        iQBankIds;
    appCodeDom.select2();
    appCodeDom.on('change', function(){
        appCode = appCodeDom.val();
        inParams.iAppCode  = appCode;
        outParams.iAppCode = appCode;
        inList.addDataGridParams(inParams);
        outList.addDataGridParams(outParams);
    });
    inList.initDataGrid(baseUrl + 'Res/getQBankList', {"queryParams": inParams})
        .setSelectThenEnable(['bt_out'], [], 'bs');
    outList.initDataGrid(baseUrl + 'Res/getQBankList', {"queryParams": outParams}, DataGrid2)
        .setSelectThenEnable(['bt_in'], [], 'bs', DataGrid2);
    $('#bt_in').click(function(){
        iQBankIds = outList.getDataGridSelectedFields('iQBankId');      //获取所选行未关联题库ID
        contactQuestion('in');
    });
    $('#bt_out').click(function(){
        iQBankIds = inList.getDataGridSelectedFields('iQBankId');       //获取所选行已关联题库ID
        contactQuestion('out');
    });
    //根据类型进行关联或取消关联操作
    function contactQuestion(type) {
        $.post(baseUrl + 'Res/contactAppQueBank', {iSchoolId: schoolId, iAppCode: appCode, iQBankIds: iQBankIds, type: type}, function (result) {
            $('body').append(result);
            inList.dataGridReload();
            outList.dataGridReload();
            $('#bt_in, #bt_out').setButtonDisable('bs');                    //按钮变为禁用状态
        });
    }
});